package com.jalarbee.core.bootik.bon.business;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.BatchSize;

import com.jalarbee.core.bootik.client.business.Client;
import com.jalarbee.core.bootik.order.business.Order;
import com.jalarbee.core.business.BaseEntity;

@Entity
public class Bon extends BaseEntity {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	
	@ManyToOne(optional=false, fetch=FetchType.LAZY)
	private Client client;
	
	@OneToMany(mappedBy = "bon")
	@BatchSize(size=2)
	private List<Order> orders ;
	
	@Enumerated(EnumType.STRING)
	private Status status;
	
	@Temporal(value=TemporalType.DATE)
//	@Column(updatable = false, insertable = true)
//	@org.hibernate.annotations.Generated(GenerationTime.INSERT)
	private Date creationDate;
	
	@Temporal(value=TemporalType.DATE)
//	@Column(updatable = false, insertable = true)
//	@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
	private Date lastModified;
	
	@Temporal(value=TemporalType.DATE)
	private Date openFromDate;
	
	@Temporal(value=TemporalType.DATE)
	private Date openTillDate;
	
	@Temporal(value=TemporalType.DATE)
	private Date closingDate;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	public List<Order> getOrders() {
		return orders;
	}
	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public Date getOpenFromDate() {
		return openFromDate;
	}
	public void setOpenFromDate(Date openFromDate) {
		this.openFromDate = openFromDate;
	}
	public Date getOpenTillDate() {
		return openTillDate;
	}
	public void setOpenTillDate(Date openTillDate) {
		this.openTillDate = openTillDate;
	}
	public Date getClosingDate() {
		return closingDate;
	}
	public void setClosingDate(Date closingDate) {
		this.closingDate = closingDate;
	}
	public Date getLastModified() {
		return lastModified;
	}
	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}
	
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Bon)) {
			return false;
		}
		Bon tmp = (Bon) obj;
		if(id == tmp.getId()) {
			return true;
		}
		return false;
	}
	@Override
	public int hashCode() {
		return id * 32 + client.hashCode();
	}
	@Override
	public String toString() {
		return "bon " + id + " for " + client.getProfile().getFirstName() + " with " + orders.size() + " orders"; 
	}

}
